package com.ysd.shiro.controller;


import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ysd.common.utils.OrdersVo;
import com.ysd.shiro.entity.User;
import com.ysd.shiro.fegin.OrderFeginClient;
import com.ysd.shiro.service.UserBackService;
import com.ysd.user.utils.PageUtil;
import com.ysd.user.utils.ResponeMessageUtil;


@Controller
@RequestMapping("/back/")
@ResponseBody
public class BackUserController {
	
	@Autowired
	private UserBackService userService;
	
	@Autowired
	private OrderFeginClient orderFeginClient;
	
	
	/**
	 * 查询出所有的用户
	 * @return
	 */
	@RequestMapping("/queryAllUser")
	public ResponeMessageUtil queryAllUser(int page,int limit,String name,String gender,String isAdmin) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(userService.queryUserCounts(name, gender,isAdmin), userService.queryAllUser(setValue,limit,name,gender,isAdmin));
	}
	
	/**
	 * 添加一个用户
	 * @param isAdminStatus
	 * @param user
	 * @return
	 */
	@RequestMapping("/addUser")
	public Boolean addUser(String isAdminStatus, User user) {
		if(isAdminStatus != null && isAdminStatus != "" && isAdminStatus.equals("on")) {
				user.setIsAdmin(1);
		}else {
			user.setIsAdmin(0);
		}
		return userService.addUser(user);
	}
	
	
	/**
	 * 编辑用户的基础信息
	 * @param isAdminStatus
	 * @param user
	 * @return
	 */
	@RequestMapping("/editUserById")
	public Boolean editUserById(String isAdminStatus, User user) {
		//获得传入的是否为管理员状态如是者传的值为on，再就行赋值为对应的1（是）0（不是）
		if(isAdminStatus != null && isAdminStatus != "" && isAdminStatus.equals("on")) {
			user.setIsAdmin(1);
		}else {
			user.setIsAdmin(0);
		}
		//获得业务层返回的执行结果，修改成功者返回为true
		System.out.println(user);
		return userService.editUserById(user);
	}
	
	/**
	 * 进行批量的删除操作
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delAllUserByIds")
	public Boolean delAllUserByIds(@RequestParam("ids")String ids) {
		return userService.delAllUserByIds(ids);
	}
	
	/**
	 * 删除单个的用户
	 * @param id
	 * @return
	 */
	@RequestMapping("/delUserById/{id}")
	public Boolean delUserById(@PathVariable("id") int id) {
		return userService.delUserById(id);
	}
	
	/**
	 * 编辑用户的是否锁定账号的状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editUserIsLockById/{userId}/{valid}")
	public Boolean editUserIsLockById(@PathVariable("userId") Integer id,@PathVariable("valid") String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
		}else {
			number = 0;
		}
		return userService.editUserIsLockById(id,number);
	}
	
	@RequestMapping("/editUserAdminStatusById/{userId}/{valid}")
	public Boolean editUserAdminStatusById(@PathVariable("userId") Integer userId,@PathVariable("valid") String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
		}else {
			number = 0;
		}
		return userService.editUserAdminStatusById(userId,number);
	}
	
	/**
	 * 查询所有的订单根据用户id，用于在用户管理页面删除操作做出提示，看看该用户是否有订单关联
	 * @return
	 */

	 @RequestMapping("/queryAllOrderInfoByUserIds")
	 public Boolean queryAllOrderInfoByUserIds(@RequestParam("ids")String ids) { 
		 OrdersVo order = new OrdersVo();
		 order.setfUid(ids);
		 Map<String,Object> map = orderFeginClient.showAll(order,1,10);
		 Boolean flag = false;
		 if(map != null) {
			 flag = false;
		 }else {
			 flag = true;
		 }
		 return  flag; 
     }
	 
	
	/**
	 * 在个人信息页编辑基础的登录者用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/editUserBasicInformationById")
	public Boolean editUserBasicInformationById(User user) {
		return userService.editUserBasicInformationById(user);
	}
	
	/**
	 * 查询出所有用户的信息根据name来查询如查询出有者给提示信息用户名不能重复
	 * @param newName
	 * @return
	 */
	@RequestMapping("/queryAllUserByNameWitchVerify/{newName}")
	public Boolean queryAllUserByNameWitchVerify(@PathVariable("newName") String newName) {
		return userService.queryAllUserByNameWitchVerify(newName);
	}
	
	/**
	 * 查询出用户的信息用于展示到留言页面的下拉框内
	 * @return
	 */
	@RequestMapping("/queryAllUserWitchMessageSelect")
	@ResponseBody
	public List<User> queryAllUserWitchMessageSelect(){
		return userService.queryAllUserWitchOrderIndoSelect();
	}
}
